package com.pda.gongpin.presenter;


import com.pda.gongpin.bean.ScanResultBean;
import com.pda.gongpin.bean.ScatteredInfoBean;
import com.pda.gongpin.bean.StockAbnormalBean;
import com.pda.gongpin.bean.request.InventoryConfirmRequest;
import com.pda.gongpin.bean.request.QRcodeRequest;
import com.pda.gongpin.contract.FullInventoryDetailsContract;
import com.pda.gongpin.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.net.bean.BaseRowsAppEntity;
import com.wangshen.base.net.bean.ImageEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.io.File;
import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class FullInventoryDetailsPresenter extends BasePresenter<FullInventoryDetailsContract.View> implements FullInventoryDetailsContract.Presenter{

    @Override
    public void getWholePalletInfo(String taskDetailId) {
        NetUtils.geApi().getWholePalletInfo(taskDetailId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ScatteredInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScatteredInfoBean> scatteredInfoBeanBaseAppEntity) throws Exception {
                        getView().setData(scatteredInfoBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void wholePalletInventoryConfirm(InventoryConfirmRequest request) {
        NetUtils.geApi().wholePalletInventoryConfirm(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ScatteredInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScatteredInfoBean> scatteredInfoBeanBaseAppEntity) throws Exception {
                        getView().showMessage(scatteredInfoBeanBaseAppEntity.getMsg());
                        getView().finishActivity();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void scanTheQRCodeToVerify(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        getView().setScan(stringBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void upload(String path) {
        File file=new File(path);
        RequestBody body=RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
        NetUtils.geApi().upload(part)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<ImageEntity>() {
                    @Override
                    public void accept(ImageEntity listBaseAppEntity) throws Exception {
                        getView().setImageEntity(listBaseAppEntity);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void countBackInWarehouse(String taskDetailId) {
        NetUtils.geApi().countBackInWarehouse(taskDetailId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<StockAbnormalBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<StockAbnormalBean>> listBaseRowsAppEntity) throws Exception {
                        getView().showMessage("回库成功！");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
